package com.zt.rongda.salary.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zt.rongda.salary.inter.SalaryEmpQuotaMapper;
import com.zt.rongda.salary.model.SalaryEmpQuota;
import com.zt.servlet.ReqBean;
import com.zt.system.model.BusinessException;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class SalaryEmpQuotaService extends BaseService {

	@Autowired
	private SalaryEmpQuotaMapper salaryEmpQuotaMapper;

	/**
	 * 查询
	 *
	 * @param reqBean
	 * @return
	 */
	public JSONObject getTable(ReqBean reqBean) {
		SqlBuilder sqlBuilder = new SqlBuilder("");
		sqlBuilder.appendSql("select seq.*,e.EMP_NAME,qc.QUOTA_CONFIG_NAME from SALARY_EMP_QUOTA  seq ");
		sqlBuilder.appendSql(" LEFT JOIN QUOTA_CONFIG qc on seq.QUOTA_CONFIG_TYPE=qc.QUOTA_CONFIG_ID");
		sqlBuilder.appendSql(" LEFT JOIN EMPLOYEE e on seq.EMP_ID=e.EMP_ID where 1=1 ");
		sqlBuilder.addWhere("qc.QUOTA_CONFIG_ID", reqBean.getInfo("quotaConfigId"));
		sqlBuilder.addWhereLike2("e.EMP_NAME", reqBean.getInfo("empName"));
		sqlBuilder.orderBy("order by seq.EMP_ID");
		return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
	}
	
	/**
	 * 通过id查询
	 *
	 * @param empId
	 * @param workStepId
	 * @return
	 */
	public SalaryEmpQuota getById(String empId, String quotaConfigType) {
		return salaryEmpQuotaMapper.selectByPrimaryKey(empId, quotaConfigType);
	}

	/**
	 * 新增
	 *
	 * @param salaryEmpQuota
	 */
	public void add(SalaryEmpQuota salaryEmpQuota) {
		salaryEmpQuota.setEmpId(stringId());// 手动设置Id
		salaryEmpQuota.setQuotaConfigType(stringId());// 手动设置Id

		salaryEmpQuota.setMakeTime(new Date());
		salaryEmpQuota.setModifyTime(salaryEmpQuota.getMakeTime());
		salaryEmpQuotaMapper.insert(salaryEmpQuota);
	}

	/**
	 * 修改
	 *
	 * @param salaryEmpQuota
	 */
	public void update(SalaryEmpQuota salaryEmpQuota) {
		String empId = salaryEmpQuota.getEmpId();
		String quotaConfigType = salaryEmpQuota.getQuotaConfigType();
		if (StringUtil.isEmpty(empId) || StringUtil.isEmpty(quotaConfigType)) {
			throw new BusinessException("获取主键失败");
		}

		salaryEmpQuota.setModifyTime(new Date());
		salaryEmpQuotaMapper.updateByPrimaryKeySelective(salaryEmpQuota);
	}

	/**
	 * 删除
	 *
	 * @param empId
	 * @param workStepId
	 */
	public void delete(String empId, String workStepId) {
		if (StringUtil.isEmpty(empId) || StringUtil.isEmpty(workStepId)) {
			throw new BusinessException("获取主键失败");
		}
		salaryEmpQuotaMapper.deleteByPrimaryKey(empId, workStepId);
	}

	/**
	 * 新增一个员工，生成员工对应全部的工序定额
	 * 
	 * @param empId
	 * @param userId
	 */
	public void createEmpQuota(String empId,String userId) {
		Date date = new Date();
		SqlBuilder sql = new SqlBuilder(" select DISTINCT QUOTA_CONFIG_TYPE ,PUBLISH_TYPE_NAME from QUOTA_CONFIG");
		JSONArray quotaConfigArray = this.query(sql);
		for (int i = 0; i < quotaConfigArray.size(); i++) {
			JSONObject quotaConfig = quotaConfigArray.getJSONObject(i);
			String quotaConfigType = quotaConfig.getString("quotaConfigType");
			SalaryEmpQuota salaryEmpQuota = new SalaryEmpQuota();
			salaryEmpQuota.setEmpId(empId);
			salaryEmpQuota.setQuotaConfigType(quotaConfigType);
			salaryEmpQuota.setEmpQuota(0.0);
			salaryEmpQuota.setOperator(userId);
			salaryEmpQuota.setMakeTime(date);
			salaryEmpQuota.setModifyTime(date);
			salaryEmpQuotaMapper.insert(salaryEmpQuota);
		}
	}
}